package com.kmis.web;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author moc
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ApiResult extends HashMap<String, Object> {

    private final static int CODE_SUCCESS = 200;
    private final static int CODE_ERROR = 400;
    private Boolean success;
    private Integer code;
    private String message;
    private Map<String,Object> data;


    public ApiResult(boolean success, int code, String message, Map<String,Object> data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static ApiResult error(String msg) {
        return error(500, msg);
    }

    public static ApiResult error(int code, String msg) {
        ApiResult r = new ApiResult();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static ApiResult ok(String msg) {
        ApiResult r = new ApiResult();
        r.put("code", CODE_SUCCESS);
        r.put("msg", msg);
        return r;
    }

    public static ApiResult ok(Map<String, Object> map) {
        ApiResult r = new ApiResult();
        r.put("code", CODE_SUCCESS);
        r.putAll(map);
        return r;
    }


    public ApiResult put(String key, Object value) {
        super.put(key,value);
        return this;
    }
}
